a20fb9
@@ -64,6 +64,7 @@
public class RegionServerDynamicMetrics implements Updater {
     LogFactory.getLog(RegionServerDynamicStatistics.class);
   
   private boolean reflectionInitialized = false;
+  private boolean needsUpdateMessage = false;
   private Field recordMetricMapField;
   private Field registryMetricMapField;
 
@@ -102,14 +103,7 @@
public class RegionServerDynamicMetrics implements Updater {
     MetricsLongValue m = (MetricsLongValue)registry.get(name);
     if (m == null) {
       m = new MetricsLongValue(name, this.registry);
-      try {
-        if (updateMbeanInfoIfMetricsListChanged != null) {
-          updateMbeanInfoIfMetricsListChanged.invoke(this.rsDynamicStatistics,
-              new Object[]{});
-        }
-      } catch (Exception e) {
-        LOG.error(e);
-      }
+      this.needsUpdateMessage = true;
     }
     m.set(amt);
   }
@@ -121,14 +115,7 @@
public class RegionServerDynamicMetrics implements Updater {
     MetricsTimeVaryingRate m = (MetricsTimeVaryingRate)registry.get(name);
     if (m == null) {
       m = new MetricsTimeVaryingRate(name, this.registry);
-      try {
-        if (updateMbeanInfoIfMetricsListChanged != null) {
-          updateMbeanInfoIfMetricsListChanged.invoke(this.rsDynamicStatistics,
-              new Object[]{});
-        }
-      } catch (Exception e) {
-        LOG.error(e);
-      }
+      this.needsUpdateMessage = true;
     }
     if (numOps > 0) {
       m.inc(numOps, amt);
@@ -141,7 +128,7 @@
public class RegionServerDynamicMetrics implements Updater {
    */
   @SuppressWarnings("rawtypes")
   public void clear() {
-    
+    this.needsUpdateMessage = true;
     // If this is the first clear use reflection to get the two maps that hold copies of our 
     // metrics on the hadoop metrics side. We have to use reflection because there is not 
     // remove metrics on the hadoop side. If we can't get them then clearing old metrics 
@@ -212,6 +199,21 @@
public class RegionServerDynamicMetrics implements Updater {
           value.getSecond().getAndSet(0));
     }
 
+    // If there are new metrics sending this message to jmx tells it to update everything.
+    // This is not ideal we should just move to metrics2 that has full support for dynamic metrics.
+    if (needsUpdateMessage) {
+      try {
+        if (updateMbeanInfoIfMetricsListChanged != null) {
+          updateMbeanInfoIfMetricsListChanged.invoke(this.rsDynamicStatistics,
+              new Object[]{});
+        }
+      } catch (Exception e) {
+        LOG.error(e);
+      }
+      needsUpdateMessage = false;
+    }
+
+
     synchronized (registry) {
       // Iterate through the registry to propagate the different rpc metrics.
       for (String metricName : registry.getKeyList() ) {
